//
//  GYSpeechSynthesizerTool.m
//  LockManagement
//
//  Created by 丁燕春 on 2018/9/9.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYSpeechSynthesizerTool.h"
#import <AVFoundation/AVSpeechSynthesis.h>

@interface GYSpeechSynthesizerTool()<AVSpeechSynthesizerDelegate>

@end

@implementation GYSpeechSynthesizerTool

- (void)textToSpeechConversion:(NSString *)text {
    //初始化对象
    AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc]init];
    av.delegate = self;
    AVSpeechUtterance*utterance = [[AVSpeechUtterance alloc]initWithString:text];//需要转换的文字
    
    utterance.rate=0.5;// 设置语速，范围0-1，注意0最慢，1最快；AVSpeechUtteranceMinimumSpeechRate最慢，AVSpeechUtteranceMaximumSpeechRate最快
    
    AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置发音，这是中文普通话
    
    utterance.voice= voice;
    
    [av speakUtterance:utterance];//开始
}

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance{
    
    NSLog(@"---开始播放");
    
}
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance{
    
    NSLog(@"---完成播放");
    
}
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance{
    
    NSLog(@"---播放中止");
    
}
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance{
    
    NSLog(@"---恢复播放");
    
}
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance{
    
    NSLog(@"---播放取消");
    
}

@end
